package com.csx.orm.mybatis.mapper;

import com.csx.orm.mybatis.entity.User;
import com.csx.orm.mybatis.mapper.provider.UserMapperProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 *
 * @author cuisongxu
 * @date 2023/11/18 周六 10:36
 */

@Mapper
public interface UserAnnotationMapper {

    /**
     * 1. 不带参数
     */
    @Select("select * from user")
    List<User> findAll();

    /**
     * 2. 带有参数
     */
    @Select("select * from user where id = #{id}")
    User findById(Long id);


    /**
     * 3. 复杂sql
     */
    @Select("<script>select * from user " +
            "<where> " +
            "<if test='id != null'> and id = #{id} </if>" +
            "<if test='firstName != null'> and first_name like concat('%', #{firstName}, '%') </if>" +
            "</where>" +
            "</script>")
    List<User> findUsersByCondition(User condition);


    /**
     * 4. 结果集映射: 通过 @Results 注解定义 ResultMap
     */
    @Select("select * from user")
    @Results(id =  "userResultsId", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "firstName", column = "first_name"),
            @Result(property = "lastName", column = "last_name")
    })
    List<User> findUsersResults();


    /**
     * 5. 结果集映射复用: 这里通过引用 上面定义的 userResultsId 这个resultMap
     */
    @Select("select * from user")
    @ResultMap("userResultsId")
    @Options(useCache = true, fetchSize = -1)
    List<User> findAllResultsReuse();


    /**
     * 6. 插入数据(带有主键): 根据debug发现,即使没有添加主键ID,也能够完成自增
     */
    @Insert("insert into user(id, first_name, last_name, age, city) values (#{id}, #{firstName}, #{lastName}, #{age}, #{city})")
    int save(User user);

    /**
     * 7. 插入数据(主键自增): 通过 Options 注解使用主键自增
     */
    @Insert("insert into user(first_name, last_name, age, city) values (#{firstName}, #{lastName}, #{age}, #{city})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int saveUseGeneratedKeys(User user);

    /**
     * 8. 修改数据
     */
    @Update("update user set first_name = #{firstName}, last_name = #{lastName} where id = #{id} ")
    int updateById(User user);

    /**
     * 9. 删除数据
     */
    @Delete("delete from user where id = #{id}")
    int deleteById(Long id);

    /**
     * 10. SelectProvider 提供 sql
     */
    @SelectProvider(type = UserMapperProvider.class, method = "findConditionSelectProvider")
    List<User> findConditionSelectProvider(User condition);

    /**
     * 11. InsertProvider 提供 sql
     */
    @InsertProvider(type = UserMapperProvider.class, method = "saveInsertProvider")
    int saveInsertProvider(User user);

    /**
     * 12. InsertProvider 提供 sql - 通过 columns
     */
    @InsertProvider(type = UserMapperProvider.class, method = "saveInsertProviderColumns")
    int saveInsertProviderColumns(User user);

    /**
     * 13. UpdateProvider 提供sql
     */
    @UpdateProvider(type = UserMapperProvider.class, method = "updateUpdateProvider")
    int updateUpdateProvider(User user);

    /**
     * 14. DeleteProvider 提供sql
     */
    @DeleteProvider(type = UserMapperProvider.class, method = "deleteDeleteProvider")
    int deleteDeleteProvider(Long id);

}
